package control;

import java.io.IOException;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import javax.portlet.PortletSession;

import org.apache.log4j.Logger;

import core.corewa.Event;
import db.dbo.ListDictionariesDBO;
import db.dbo.UserDBO;

import form.ApproverHomeForm;
import form.BaseForm;
import form.ReviewerHomeForm;

@SuppressWarnings("serial")
public class LoginController extends BaseController {
	Logger LOG = Logger.getLogger("LoginController");
	/**
	 * do nothing in here.
	 * @param request
	 * @param response
	 * @param event
	 * @return
	 * @throws PortletException
	 * @throws IOException
	 */
	public BaseForm procHome(ActionRequest request, ActionResponse response, Event event)throws PortletException, IOException {
		return null;
	}
	/**
	 * login function.
	 * @param request
	 * @param response
	 * @param event
	 * @return
	 * @throws PortletException
	 * @throws IOException
	 */
	public BaseForm procLogin(ActionRequest request, ActionResponse response, Event event)throws PortletException, IOException {
		String username = request.getParameter("txtUsername");
		String password = request.getParameter("txtPassword");
		UserDBO userDBO = new UserDBO();
		int role = userDBO.getRole(username, password);
		ListDictionariesDBO listDict = new ListDictionariesDBO();
		if(role == -1) {
			request.getPortletSession().setAttribute("errMess", 
									"<font color=\"#FF0000\">Invalid username and password!</font>", 
									PortletSession.APPLICATION_SCOPE);
			event.setNextScrId("/WEB-INF/jsp/Login.jsp");
		}
		if(role == 0) {
			event.setNextScrId("/WEB-INF/jsp/SearchWord.jsp");
			request.getPortletSession().setAttribute("username", username,PortletSession.APPLICATION_SCOPE);
	    	request.getPortletSession().setAttribute("role", role, PortletSession.APPLICATION_SCOPE);
		}
		if(role == 1) {
			event.setNextScrId("/WEB-INF/jsp/ReviewerHome.jsp");
			request.getPortletSession().setAttribute("username", username,PortletSession.APPLICATION_SCOPE);
	    	request.getPortletSession().setAttribute("role", role, PortletSession.APPLICATION_SCOPE);
	    	ReviewerHomeForm reviewerForm = new ReviewerHomeForm();
	    	
	    	reviewerForm.setListDictionaries(listDict.getListDictionariesReviewer(username));
	    	return reviewerForm;
		}
		if(role == 2) {
			event.setNextScrId("/WEB-INF/jsp/ApproverHome.jsp");
			request.getPortletSession().setAttribute("username", username,PortletSession.APPLICATION_SCOPE);
	    	request.getPortletSession().setAttribute("role", role, PortletSession.APPLICATION_SCOPE);
	    	ApproverHomeForm approverForm = new ApproverHomeForm();
	    	approverForm.setListDictionaries(listDict.getListDictionariesApprover(username));
	    	return approverForm;
		}
		if(role == 3) {
			event.setNextScrId("/WEB-INF/jsp/AdministratorHome.jsp");
			request.getPortletSession().setAttribute("username", username,PortletSession.APPLICATION_SCOPE);
	    	request.getPortletSession().setAttribute("role", role, PortletSession.APPLICATION_SCOPE);
		}
		return null;
	}
}
